<template>
  <right-template :title="componentData.title" :tabName="componentData.tabName">
    <el-form class="right-slot-machine">
      <input-normal label="转动一次时间" v-model="commonAttr.spinsTime" @change="onChangeAttr"></input-normal>
      <input-normal label="动画至少时间" v-model="commonAttr.animateTime" @change="onChangeAttr"></input-normal>
      <input-normal label="动画暂停时间" v-model="commonAttr.pauseTime" @change="onChangeAttr"></input-normal>
      <el-button type="primary"  @click.stop="onAddItem" class="margin-left-right-16 margin-bottom-10 margin-top-10"><el-icon><i class="jy-icon-plus"></i></el-icon>增加内容</el-button>
      <el-collapse v-model="activeNames">
        <el-collapse-item v-for="(item, index) in lists" :name="index+1" :key="index">
          <template #title>
            列表项{{index+1}}
            <i class="jy-icon-delete el-collapse-item__arrow" @click.stop="onDeleteItem(index)"></i>
          </template>
          <add-img-con :url="item.url" picAttr="url" :index="index" :isCrop='false' :height="100" class="margin-left-right-16"></add-img-con>
          <input-normal label="地址" v-model="item.url" @change="onUpdateItem(index)"></input-normal>
        </el-collapse-item>
      </el-collapse>
    </el-form>
  </right-template>
</template>

<script setup lang="ts">
import { useWebComponentStore } from '@/store/web/component'
import { ROTATE3D_LIST } from '@/const/web/'
import { ComponentTypesDefaultDom } from '@/store/models/component-default-data'
const useWebComponent = useWebComponentStore()

let componentData_: any
const componentData = computed(() => {
  componentData_ = useWebComponent.currentComponentData
  return componentData_
})
let commonAttr_: any
const commonAttr = computed(() => {
  commonAttr_ = useWebComponent.currentComponentData!.commonAttr
  return commonAttr_
})
const onChangeAttr = () => {
  // useWebComponent.updateCurrentComponentData({ commonAttr: commonAttr_ })
}
const activeNames = [1]
const lists = computed(() => {
  return useWebComponent.currentComponentData!.lists
})
const onAddItem = () => {
  let item: IListItem
  if(componentData_.events.rotate3d) {
    item = _.cloneDeep(ROTATE3D_LIST[0])
  } else {
    item = _.cloneDeep(ComponentTypesDefaultDom[componentData_.cid].lists[0])
  }
  if(componentData_.lists && componentData_.lists[0]) {
    item = _.cloneDeep(componentData_.lists[0])
  }
  if(item){
    useWebComponent.addComponentItem(item)
  }
}
const onUpdateItem = (index: number) => {
  useWebComponent.updateComponentItem({ index, item: componentData_.lists[index] })
}
const onDeleteItem = (index: number) => {
  useWebComponent.deleteComponentItem({index})
}
</script>